package erfgame.core.graphics.renderer.animation;

import java.util.ArrayList;
import java.util.Map;

import erfgame.core.entity.EntityType;
import erfgame.core.entity.State;

public class DelegatingByEntityTypeStateAnimationCycleFactory implements StateAnimationCycleFactory {
    
    private Map<EntityType, StateAnimationCycleFactory> children;
    
    public DelegatingByEntityTypeStateAnimationCycleFactory( Map<EntityType, StateAnimationCycleFactory> children ) {
        this.children = children;
    }
    
    public AnimationCycle getAnimationCycle(State state, EntityType entityType) {
        ArrayList<EntityType> entityTypes = new ArrayList<EntityType>();
        AnimationCycle result = null;
        while( entityTypes.size()>0 ) {
            EntityType type = entityTypes.remove(0);
            StateAnimationCycleFactory child = children.get(type);
            if( child != null ) {
                result = child.getAnimationCycle(state, type);
            }
            if( result == null ) {
                entityTypes.addAll(type.getParentTypes());
            } else {
                break;
            }
        }
        return result;
    }

}
